package testcase.app;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONArray;
import org.json.JSONException;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import api.app.Package;
import common.Host;
import common.TestData;

/**
 * 帮忙医APP获取卡套餐接口Package测试用例
 * @author gaowen
 *
 */
public class TestPackage
{
	/**
	 * 帮忙医APP获取卡套餐接口Package测试环境数据驱动
	 * @return 测试数据
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@DataProvider(name="Test")
	public static String[][] testPackageDataProvider() throws IOException, ParserConfigurationException, SAXException
	{
		Host.setup("Test");
		TestData td=new TestData("app/TestPackage.xml");
		return td.getParam();
	}
	
	/**
	 * 帮忙医APP获取卡套餐接口Package生产环境数据驱动
	 * @return 测试数据
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@DataProvider(name="Prod")
	public static String[][] prodPackageDataProvider() throws IOException, ParserConfigurationException, SAXException
	{
		Host.setup("Prod");
		TestData td=new TestData("app/TestPackage.xml");
		return td.getParam();
	}
	
	/**
	 * 帮忙医APP获取卡套餐接口Package测试环境数测试用例
	 * @param casename 用例名
	 * @param cardNo 卡号
	 * @param sex 性别
	 * @param token 用户token
	 * @throws IOException
	 * @throws JSONException
	 */
	@Test(dataProvider="Test",groups="Test")
	public void testPackageTestCase(String casename,String cardNo,String sex,String token) throws IOException, JSONException
	{
		String result=Package.get(cardNo, sex,token);
		JSONArray jsonResult;
		if (result==null||result.isEmpty())
		{
			jsonResult=null;
		}
		else
		{
			jsonResult=new JSONArray(result);
		}
		switch (casename)
		{
			case "ManSuccess":
				Assert.assertEquals(jsonResult.getJSONObject(0).getString("packageName"),"2017年全年大智慧毛建杰测试");
				break;
			case "WomanSuccess":
				Assert.assertEquals(jsonResult.getJSONObject(0).getString("packageName"),"2017年全年大智慧毛建杰测试");
				break;
			case "CardNoError":
				Assert.assertTrue(jsonResult.isNull(0));
				break;
			case "CardNoNull":
				Assert.assertTrue(jsonResult.isNull(0));
				break;
			case "SexNull":
				Assert.assertEquals(jsonResult.getJSONObject(0).getString("packageName"),"2017年全年大智慧毛建杰测试");
				break;
			case "TokenError":
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Assert.assertNull(result);
				break;
			default:
				break;
		}
	}
	
	/**
	 * 帮忙医APP获取卡套餐接口Package生产环境数测试用例
	 * @param casename 用例名
	 * @param cardNo 卡号
	 * @param sex 性别
	 * @param token 用户token
	 * @throws IOException
	 * @throws JSONException
	 */
	@Test(dataProvider="Prod",groups="Prod")
	public void prodPackageTestCase(String casename,String cardNo,String sex,String token) throws IOException, JSONException
	{
		String result=Package.get(cardNo, sex,token);
		JSONArray jsonResult;
		if (result==null||result.isEmpty())
		{
			jsonResult=null;
		}
		else
		{
			jsonResult=new JSONArray(result);
		}
		switch (casename)
		{
			case "ManSuccess":
				Assert.assertEquals(jsonResult.getJSONObject(0).getString("packageName"),"680礼品卡--男（上海）");
				break;
			case "WomanSuccess":
				Assert.assertEquals(jsonResult.getJSONObject(0).getString("packageName"),"680礼品卡--女已婚（上海）");
				break;
			case "CardNoError":
				Assert.assertTrue(jsonResult.isNull(0));
				break;
			case "CardNoNull":
				Assert.assertTrue(jsonResult.isNull(0));
				break;
			case "SexNull":
				Assert.assertEquals(jsonResult.getJSONObject(0).getString("packageName"),"680礼品卡--男（上海）");
				break;
			case "TokenError":
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Assert.assertNull(result);
				break;
			default:
				break;
		}
	}
}
